# -*- coding:utf-8 -*- 
from google.appengine.ext import webapp
from model import *
from mywork import * 
from google.appengine.ext import db
import md5
import urllib
import random
class index(webapp.RequestHandler):
    def get(self):
        #try:
        self.id_group=urllib.unquote(self.request.path.split('/')[-1])
        data=entry.all().filter('id_group =', int(self.id_group)).get()
        img=data.photos
        data_n=entry.all().order('-id_group').get()
        nowid=data_n.id_group  
        num_tuijian=random.sample (range(int(nowid)),6)
        dat_tj=[]
        for i in num_tuijian:
            tuijian=entry.all().filter('id_group =', int(i)).get()
            #for i in tuijian.photos:
            dat_tj.append(tuijian)
        self.response.headers['Cache-Control'] = "public,max-age=10000"
        self.response.out.write(render('index.html',{'data':data,'img':img,'tuijian':dat_tj}))
        #except:
         #   self.response.out.write(render('index.html',{'data':'','img':''}))
        
class updata(webapp.RequestHandler):
    def get(self):
        self.response.out.write(render('up.html',{}))
    def post(self):
        self.photo_descrip   =code_data(self.request.get('photo_descrip')) or 'no'
        self.photo_url       =code_data(self.request.get('photo_url')) or r'http://s-album.appspot.com/img/88f5db500777fad5cc4770da32c0657f'
        try:
            data=entry.all().filter('id_group =',int(self.request.get('id_group'))).get()
            data.name
        except:
            data=entry()
            data.name            =code_data(self.request.get('name')) or 'noname'
            data.title           =code_data(self.request.get('title')) or 'notitle'
            
        #pub_data        =db.DateTimeProperty(auto_now_add=True)
            data.tag             =code_data(self.request.get('tag')) or 'no'
            data.catalog         =code_data(self.request.get("nocatalog")) or 'no'
            data.name_group      =code_data(self.request.get("name_group")) or 'no'
            data.id_group        =int(self.request.get('id_group')) or '0001'
            data.put()
        photo_group(photo_entry=data,
                    photo_descrip=self.photo_descrip,
                    photo_url=self.photo_url,).put()
        self.response.out.write('post data ok')
        
class test(webapp.RequestHandler):
    def get(self):
        self.response.out.write(render('index.html',{'data':'','img':''}))
        
        
class getnowid(webapp.RequestHandler):
    def get(self):
        data=entry.all().order('-id_group').get()
        nowid=data.id_group
        self.response.out.write(nowid)  

class tag(webapp.RequestHandler):
    def get(self):
        self.tag=urllib.unquote(self.request.path.split('/')[-1])
        data=entry.all().filter('tag =',code_data(self.tag)).fetch(limit=40)     
        self.response.out.write(render('query.html',{'data':data}))
        
class catalog(webapp.RequestHandler):
    def get(self):
        self.catalog=urllib.unquote(self.request.path.split('/')[-1])
        data=entry.all().filter('catalog =',code_data(self.catalog)).fetch(limit=40)
        self.response.out.write(render('query.html',{'data':data}))

class mian_page(webapp.RequestHandler):
    def get(self):
        #self.catalog=urllib.unquote(self.request.path.split('/')[-1])
        data=entry.all().order('-id_group').fetch(limit=63)
        self.response.out.write(render('query.html',{'data':data}))

class feed(webapp.RequestHandler):
    def get(self):
        #self.catalog=urllib.unquote(self.request.path.split('/')[-1])
        data=entry.all().order('-id_group').fetch(limit=40)
	self.response.headers['Content-Type'] = 'application/atom+xml'
	#self.response.headers['Content-Type']='text/xml'
        self.response.out.write(render('atom.xml',{'data':data}))
